from datetime import datetime
from typing import List

from sqlalchemy import Integer, String, Boolean, Float, DateTime, ForeignKey, func
from sqlalchemy.orm import Mapped, mapped_column, relationship

from util.DataBaseTools import Base


class EmployeeModel(Base):
    __tablename__ = 'employee'

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    phone: Mapped[str] = mapped_column(String(20))
    email: Mapped[str] = mapped_column(String(50))
    name: Mapped[str] = mapped_column(String(50))
    sex: Mapped[int] = mapped_column(Integer)  # 0: 男 1：女
    port: Mapped[int] = mapped_column(Integer)
    status: Mapped[int] = mapped_column(Integer)  # 0:在职 1：离职
    entry_date: Mapped[str] = mapped_column(String(8))
    leave_date: Mapped[str] = mapped_column(String(8))
    occurtime: Mapped[datetime] = mapped_column(DateTime, default=func.now())

    def __repr__(self):
        return f'id: {self.id}, phone: {self.phone}, email: {self.email}, name: {self.name}, sex: {self.sex}, port: {self.port}, status: {self.status}, entry_date: {self.entry_date}, leave_date: {self.leave_date}'


class StudentModel(Base):
    __tablename__ = 'student'

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    name: Mapped[str] = mapped_column(String(50))
    address: Mapped[str] = mapped_column(String(255))
    age: Mapped[str] = mapped_column(String(8))
    sex: Mapped[int] = mapped_column(Integer)
    class_id: Mapped[int] = mapped_column(Integer, ForeignKey('class.id'))
    phone: Mapped[str] = mapped_column(String(20))
    email: Mapped[str] = mapped_column(String(50))
    school: Mapped[str] = mapped_column(String(100))
    school_type: Mapped[int] = mapped_column(Integer)
    major: Mapped[str] = mapped_column(String(50))
    graduation_date: Mapped[str] = mapped_column(String(8))
    has_graduation_cert: Mapped[bool] = mapped_column(Boolean)
    is_monitor: Mapped[int] = mapped_column(Integer)
    status: Mapped[int] = mapped_column(Integer)
    contact_person_phone: Mapped[str] = mapped_column(String(20))
    sale_id: Mapped[int] = mapped_column(Integer, ForeignKey('employee.id'))
    source: Mapped[int] = mapped_column(Integer)
    referrer: Mapped[str] = mapped_column(String(255))
    pay_type: Mapped[int] = mapped_column(Integer)
    loan_amount: Mapped[float] = mapped_column(Float)
    pay_amount: Mapped[float] = mapped_column(Float)
    occurtime: Mapped[datetime] = mapped_column(DateTime, default=func.now())

    class_info: Mapped['ClassModel'] = relationship('ClassModel', lazy=None, primaryjoin=('StudentModel.class_id == ClassModel.id'))
    sale_info: Mapped['EmployeeModel'] = relationship('EmployeeModel', lazy=None, primaryjoin=('StudentModel.sale_id == EmployeeModel.id'))

    def __repr__(self):
        return f'id: {self.id}, name: {self.name}, address: {self.address}, age: {self.age}, sex: {self.sex}, class_id: {self.class_id}, phone: {self.phone}, email: {self.email}, school: {self.school}, school_type: {self.school_type}, major:{self.major}, graduation_date: {self.graduation_date}, has_graduation_cert: {self.has_graduation_cert}, is_monitor: {self.is_monitor}, status: {self.status}, contact_person_phone: {self.contact_person_phone}, sale_id: {self.sale_id}, source: {self.source}, referrer: {self.referrer}, pay_type: {self.pay_type}, loan_amount: {self.loan_amount}, pay_amount: {self.pay_amount}, class_info: {self.class_info}, sale_info: {self.sale_info}'


class CourseModel(Base):
    __tablename__ = 'course'

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    course_type: Mapped[int] = mapped_column(Integer)
    course_name: Mapped[str] = mapped_column(String(50))
    course_content_id: Mapped[int] = mapped_column(Integer)

    def __repr__(self):
        return f'id: {self.id}, course_type: {self.course_type}, course_name: {self.course_name}, course_content_id: {self.course_content_id}'


class ClassModel(Base):
    __tablename__ = 'class'

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    class_type: Mapped[int] = mapped_column(Integer)
    class_name: Mapped[str] = mapped_column(String(50))
    course_id: Mapped[int] = mapped_column(Integer, ForeignKey('course.id'))
    teacher_id: Mapped[int] = mapped_column(Integer, ForeignKey('employee.id'))
    lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey('employee.id'))
    start_date: Mapped[str] = mapped_column(String(8))
    end_date: Mapped[str] = mapped_column(String(8))
    status: Mapped[int] = mapped_column(Integer)

    course_info: Mapped['employee'] = relationship('CourseModel', lazy=None, primaryjoin=('ClassModel.course_id == CourseModel.id'))
    teacher_info: Mapped['employee'] = relationship('EmployeeModel', lazy=None, primaryjoin=('ClassModel.teacher_id == EmployeeModel.id'))
    lecturer_info: Mapped['employee'] = relationship('EmployeeModel', lazy=None, primaryjoin=('ClassModel.lecturer_id == EmployeeModel.id'))
    students: Mapped[List['student']] = relationship('StudentModel', lazy=None, uselist=True, primaryjoin=('ClassModel.id == StudentModel.class_id'))
    def __repr__(self):
        return f'id: {self.id}, class_type: {self.class_type}, class_name: {self.class_name}, course_id: {self.course_id}, teacher_id: {self.teacher_id}, lecturer_id: {self.lecturer_id}, start_date: {self.start_date}, end_date: {self.end_date}, status: {self.status}, course_info: {self.course_info}, teacher_info: {self.teacher_info}, lecturer_info: {self.lecturer_info}, students: {self.students}'


class AttendanceModel(Base):
    __tablename__ = 'attendance'

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    attend_date: Mapped[str] = mapped_column(String(8))
    class_id: Mapped[int] = mapped_column(Integer)
    student_id: Mapped[int] = mapped_column(Integer, ForeignKey('student.id'))
    is_normal: Mapped[bool] = mapped_column(Boolean)
    abnormal_type: Mapped[int] = mapped_column(Integer)
    remark: Mapped[str] = mapped_column(String(8))

    student_info: Mapped['StudentModel'] = relationship('StudentModel', lazy=None, primaryjoin=('AttendanceModel.student_id == StudentModel.id'))

    def __repr__(self):
        return f'id: {self.id}, attend_date: {self.attend_date}, class_id: {self.class_id}, student_id: {self.student_id}, is_normal: {self.is_normal}, abnormal_type: {self.abnormal_type}, remark: {self.remark}, student_info: {self.student_info}'


class CourseStageModel(Base):
    __tablename__ = 'course_stage'

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    course_content_id: Mapped[int] = mapped_column(Integer)
    course_stage_name: Mapped[str] = mapped_column(String(50))
    sort_id: Mapped[int] = mapped_column(Integer)

    def __repr__(self):
        return f'id: {self.id}, course_content_id: {self.course_content_id}, course_stage_name: {self.course_stage_name}, sort_id: {self.sort_id}'


class CourseStageSubjectModel(Base):
    __tablename__ = 'course_stage_subject'

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    course_stage_id: Mapped[int] = mapped_column(Integer)
    subject_id: Mapped[int] = mapped_column(Integer)
    sort_id: Mapped[int] = mapped_column(Integer)

    def __repr__(self):
        return f'id: {self.id}, course_stage_id: {self.course_stage_id}, subject_id: {self.subject_id}, sort_id: {self.sort_id}'


class EmploymentModel(Base):
    __tablename__ = 'employment'

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    class_id: Mapped[int] = mapped_column(Integer)
    student_id: Mapped[int] = mapped_column(Integer)
    company_name: Mapped[str] = mapped_column(String(50))
    work_location: Mapped[str] = mapped_column(String(100))
    station: Mapped[str] = mapped_column(String(50))
    entry_date: Mapped[str] = mapped_column(String(8))
    salary: Mapped[float] = mapped_column(Float)
    offer: Mapped[str] = mapped_column(String(255))
    is_entry: Mapped[int] = mapped_column(Integer)
    remark: Mapped[str] = mapped_column(String(255))
    occurtime: Mapped[datetime] = mapped_column(DateTime, default=func.now())

    def __repr__(self):
        return f'id: {self.id}, class_id: {self.class_id}, student_id: {self.student_id}, company_name: {self.company_name}, work_location: {self.work_location}, station: {self.station}, entry_date: {self.entry_date}, salary: {self.salary}, offer: {self.offer}, is_entry: {self.is_entry}'


class InterviewModel(Base):
    __tablename__ = 'interview'

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    class_id: Mapped[int] = mapped_column(Integer)
    student_id: Mapped[int] = mapped_column(Integer)
    interview_type: Mapped[int] = mapped_column(Integer)
    company_name: Mapped[str] = mapped_column(String(50))
    work_location: Mapped[str] = mapped_column(String(100))
    interview_time: Mapped[datetime] = mapped_column(DateTime)
    interview_rounds: Mapped[int] = mapped_column(Integer)
    interview_result: Mapped[int] = mapped_column(Integer)
    interview_record: Mapped[str] = mapped_column(String(255))
    lecturer_id: Mapped[int] = mapped_column(Integer)
    lecturer_comment: Mapped[str] = mapped_column(String)
    occurtime: Mapped[datetime] = mapped_column(DateTime, default=func.now())

    def __repr__(self):
        return f'id: {self.id}, class_id: {self.class_id}, student_id: {self.student_id}, interview_type: {self.interview_type}, company_name: {self.company_name}, work_location: {self.work_location}, interview_time: {self.interview_time}, interview_rounds: {self.interview_rounds}, interview_result: {self.interview_result}, interview_record: {self.interview_record}, lecturer_id: {self.lecturer_id}, lecturer_comment: {self.lecturer_comment}'


class InterviewContentModel(Base):
    __tablename__ = 'interview_content'

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    interview_id: Mapped[int] = mapped_column(Integer)
    question: Mapped[str] = mapped_column(String)
    answer: Mapped[str] = mapped_column(String)


class NoticeModel(Base):
    __tablename__ = 'notice'

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    user_id: Mapped[int] = mapped_column(Integer)
    notice_topic: Mapped[str] = mapped_column(String(100))
    notice_content: Mapped[str] = mapped_column(String)
    notice_members: Mapped[str] = mapped_column(String)
    notice_members_name: Mapped[str] = mapped_column(String)
    notice_copy_members: Mapped[str] = mapped_column(String)
    notice_copy_members_name: Mapped[str] = mapped_column(String)
    status: Mapped[int] = mapped_column(Integer)
    occurtime: Mapped[datetime] = mapped_column(DateTime, default=func.now())

    def __repr__(self):
        return f'id: {self.id}, user_id: {self.user_id}, notice_topic: {self.notice_topic}, notice_content: {self.notice_content}, notice_members: {self.notice_members}, notice_members_name: {self.notice_members_name}, notice_copy_members: {self.notice_copy_members}, notice_copy_members_name: {self.notice_copy_members_name} status: {self.status}'


class SubjectModel(Base):
    __tablename__ = 'subject'

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    subject_id: Mapped[int] = mapped_column(Integer)
    subject_name: Mapped[str] = mapped_column(String(50))

    def __repr__(self):
        return f'id: {self.id}, subject_id: {self.subject_id}, subject_name: {self.subject_name}'

class SubjectSectionModel(Base):
    __tablename__ = 'subject_section'

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    subject_id: Mapped[int] = mapped_column(Integer)
    section_id: Mapped[int] = mapped_column(Integer)
    section_name: Mapped[str] = mapped_column(String(50))
    section_type: Mapped[int] = mapped_column(Integer)
    section_url: Mapped[str] = mapped_column(String(100))
    section_test_id: Mapped[int] = mapped_column(Integer)
    sort_id: Mapped[int] = mapped_column(Integer)

    def __repr__(self):
        return f'id: {self.id}, subject_id: {self.subject_id}, section_id: {self.section_id}, section_name: {self.section_name}, section_type: {self.section_type}, section_url: {self.section_url}, section_test_id: {self.section_test_id}, sort_id: {self.sort_id}'


